const fs = require("fs")
const path = require("path")
const mime = require('mime');
let data
function readFile(filePathName) {
    return new Promise((resolve, reject) => {
        fs.readFile(filePathName, (err, data) => {
            if (err) {
                reject(err)
            }
            resolve(data)
        })
    })
}
async function readStaticFile(filePathName) {
    let ext = path.parse(filePathName).ext
    let mimeType = mime.getType(ext);
    if (fs.existsSync(filePathName)) {
        if (ext) {
            data = await readFile(filePathName.replace(/\//g, '/'))
        } else {
            data = await readFile(path.join(filePathName, './index.html').replace(/\\/g, '/'))
        }
    } else {
        data = 'not found page'
    }
    return {
        data,
        mimeType
    }
}
module.exports = readStaticFile